package com.example.opengldemo.mirror;

import android.opengl.GLES30;

import com.example.opengldemo.util.MatrixState;

public class BallFromControl {
    Ball btv ;
    float startY ;
    float currentY ;

    public static final float TIME_SPAN=0.05f;
    public static final float G=0.8f;
    public static final float FLOOR_Y=-2.0f;
    public static final float UNIT_SIZE=0.8f;
    public static final float BALL_SCALE=1.0f;
    public static final float ANGLE_SPAN=11.25f;
    float timeLive=0;
    float vy=0;
    public BallFromControl(Ball btv,float startYIn){
        this.btv=btv;
        this.startY=startYIn;
        this.currentY=startYIn;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    timeLive+=TIME_SPAN;
                    float tempCurrY=startY-0.5f*G*timeLive*timeLive+ vy*timeLive;
                    if(tempCurrY<=FLOOR_Y){
                        startY=FLOOR_Y;
                        vy=-(vy-G*timeLive)*0.8f;
                        timeLive=0;
                        if(vy<0.35f){
                            currentY=FLOOR_Y;
                            break;
                        }
                    }else{
                        currentY=tempCurrY;
                    }
                    try{
                        Thread.sleep(20);
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }


    public void drawSelf(int id){
        MatrixState.pushMatrix();
        MatrixState.translate(0,UNIT_SIZE*BALL_SCALE+currentY,0);//平移变换
        btv.drawSelf(id);
        MatrixState.popMatrix();
    }

    public void drawSelfMirror(int id){
        GLES30.glFrontFace(GLES30.GL_CW);
        MatrixState.pushMatrix();
        MatrixState.scale(1,-1,1);
        MatrixState.translate(0,UNIT_SIZE*BALL_SCALE+currentY-2*FLOOR_Y,0);
        btv.drawSelf(id);
        MatrixState.popMatrix();
        GLES30.glFrontFace(GLES30.GL_CCW);
    }
}
